package com.sc.userspringboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.sc.userspringboot.pojo.Follows;
import com.sc.userspringboot.pojo.Users;
import com.sc.userspringboot.service.IFollowsService;
import com.sc.userspringboot.service.IUsersService;
import com.sc.userspringboot.utils.ResponseMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户关注关系表 前端控制器
 * </p>
 *
 * @author user
 * @since 2025-09-19 06:25:46
 */
@RestController
@RequestMapping("/follows")
public class FollowsController {
    @Autowired
    private IFollowsService followsService;

    @GetMapping("/getFollowsById")
    public ResponseMsg getFollowsById(Integer id){
        QueryWrapper<Follows> queryWrapper = new QueryWrapper<>();
        //根据follower_id查询到关注的人
        queryWrapper.eq("follower_id", id)
                .eq("is_cancel", 0);
        List<Follows> follows = followsService.list(queryWrapper);
        // 提取 follower_id 列表（关注的人的ID）
        List<Integer> followeeIdList = follows.stream()
                .map(Follows::getFollowerId)
                .collect(Collectors.toList());
        // 检查列表是否为空，避免SQL语法错误
        if (followeeIdList.isEmpty()) {
            return ResponseMsg.getInstance(ResponseMsg.SUCCESS, "查询成功", new ArrayList<Users>());
        }
        List<Follows> followers = followsService.getFollowsByIds(followeeIdList);
        return ResponseMsg.getInstance(ResponseMsg.SUCCESS, "查询成功", followers);
    }
    //获取粉丝

    @GetMapping("/getFansById")
    public ResponseMsg getFansById(Integer id){
        QueryWrapper<Follows> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("followee_id", id)
                //0为正常关注、1为取消
                .eq("is_cancel", 0);
        //根据followee_id查询到粉丝
        List<Follows> follows = followsService.list(queryWrapper);
        // 提取 followee_id 列表（关注的人的ID）
        List<Integer> followeeIdList = follows.stream()
                .map(Follows::getFolloweeId)
                .collect(Collectors.toList());
        // 检查列表是否为空，避免SQL语法错误
        if (followeeIdList.isEmpty()) {
            return ResponseMsg.getInstance(ResponseMsg.SUCCESS, "查询成功", new ArrayList<Users>());
        }
        List<Follows> fans = followsService.getFansByIds(followeeIdList);
        return ResponseMsg.getInstance(ResponseMsg.SUCCESS, "查询成功", fans);
    }

    @PostMapping("updateFollowById")
    public ResponseMsg updateFollowById(Integer followerId, Integer followeeId, Byte isCancel){
        //0为正常关注、1为取消
        QueryWrapper<Follows> wrapper = new QueryWrapper<>();
        wrapper.eq("followee_id", followeeId)
                .eq("follower_id", followerId);
        if(followsService.exists(wrapper)){
            UpdateWrapper<Follows> updateWrapper = new UpdateWrapper<>();
            //修改followee_id被关注的人
            updateWrapper.set("is_cancel", isCancel)
                    .eq("followee_id", followeeId)
                    .eq("follower_id", followerId);
            return ResponseMsg.getInstance(ResponseMsg.SUCCESS, "更新成功", followsService.update(updateWrapper));
        }
        //不存在则添加
        Follows addFollow = new Follows();
        addFollow.setFollowerId(followerId);
        addFollow.setFolloweeId(followeeId);
        addFollow.setIsCancel(isCancel);
        addFollow.setFollowTime(new Date());
        return ResponseMsg.getInstance(ResponseMsg.SUCCESS, "添加成功", followsService.save(addFollow));
    }
}
